function init(){
	var deliveryId = GetQueryString("id");// 定义发货单ID
	
	/**
	 * 审核通过和驳回的方法
	 */
	$('.button_box').on('click','button',function(){
		var approve = $(this).attr('data-id');
		var approveMemo = $('.button_box').find('textarea').val();
		if(approveMemo.trim() == ""){
			layer.msg('请先填写审批意见！', {icon:7,time:1000});
			return false;
		}
		$.ajax({
			type:"POST",
			url:"/diconet/delivery/deliveryExamine",
			data:{deliveryId:deliveryId,approveMemo:approveMemo,approve:approve},
			dataType: 'json',
			success:function(data){
				if(data.success == "T"){
					layer.msg(data.message,{icon:1,time:1000});
				}else{
					layer.msg(data.message,{icon:2,time:1000});
				}
				window.location.href = path+"/distributor/delivery_bill";
			},
			error: function(){
				layer.msg('审核失败！',{icon:5,time:1000});
			}
		});
	});
	
	loadTitle(deliveryId);
	loadProduct(deliveryId);
}

/**
 * 加载头部信息
 */
function loadTitle(deliveryId){
	$.ajax({
		type:"POST",
		url:"/diconet/delivery/deliveryDetailForTitle",
		data:{deliveryId:deliveryId},
		dataType: 'json',
		success:function(data){
			if(data != null){
				var html = "";
				html += "<p>发货单号：<span>"+data.deliveryCode+"</span></p>";
				html += "<p>客户名称：<span>"+data.purchaserName+"</span></p>";
				html += "<p>订单金额：<span>"+data.orderAmount+"</span></p>";
				var orderState;
				if(data.orderState == 1){
					orderState = '已关闭';
				}else{
					orderState = '处理中';
				}
				var paymentState;
				if(data.paymentState == 1){
					paymentState = '已结清';
				}else{
					paymentState = '付款中';
				}
				html += "<p>订单状态：<span>"+orderState+"</span></p>";
				html += "<p>付款状态：<span>"+paymentState+"</span></p>";
				html += "<p>订单日期：<span>"+data.orderTime+"</span></p>";
				$('.content').find('.order-number').html(html);
			}
		},
		error: function(){
			layer.msg('加载头部信息失败！',{icon:5,time:1000});
		}
	});
}

/**
 * 商品列表
 */
function loadProduct(deliveryId){
	$.ajax({
		type:"POST",
		url:"/diconet/delivery/deliveryDetailForProduct",
		data:{deliveryId:deliveryId},
		success:function(data){
			if(data != null){
				var html = "";
				for(var i=0; i<data.length; i++){
					var comment = data[i];
					html += "<tr>";
					html += "<td><img src='"+comment.imgUrl+"' alt=''/></td>";
					html += "<td><a href='#'>"+comment.productName+"</a></td>";
					html += "<td>";
					var productSpecs = comment.productSpecList;
					for(var j=0; j<productSpecs.length; j++){
						var productSpec = productSpecs[j];
						html += productSpec.specName+":&nbsp;&nbsp;"+productSpec.specValue+";&nbsp;&nbsp;";
					}
					html += "</td>";
					html += "<td>"+comment.orderQuantity+"</td>";
					html += "<td>"+comment.deliveryQuantity+"</td>";
					html += "<td></td></tr>";
				}
				$('.productTable').find('tbody').html(html);
			}
		},
		error: function(){
			layer.msg('加载商品信息失败！',{icon:5,time:1000});
		}
	});
}

function GetQueryString(name){
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]);
    return null;
}

init();